#!/usr/bin/python
"""
Needs:
1. PhymmBL classification file
2. KAAS annotation (KO numbers)
3. KEGG modules (has listing of KO and COG numbers)
"""

import sys
import re

korx = re.compile('(\w+)_.*_.*_.*\t(\w+).*')

phymmbl = open(sys.argv[1], "rU")
kog = open(sys.argv[2], "rU")
#mod = open(sys.argv[3], "rU")

pl = phymmbl.readlines()
kl = kog.readlines()
#ml = mod.readlines()

phydict = {}

for line in pl[1:]:
    l = line.split('\t')
    qname = l[0]
    bestmatch = l[1]
    score = l[2]
    genus = l[3]
    genus_conf = l[4]
    family = l[5]
    family_conf = l[6]
    order = l[7]
    order_conf = l[8]
    clas = l[9]
    clas_conf = l[10]
    phylum = l[11]
    phylum_conf = l[12].strip()
    phydict[qname] = (bestmatch, score, genus, genus_conf, family, family_conf,
        order, order_conf, clas, clas_conf, phylum, phylum_conf)

#test = 'EM7JFSU01A015Z'
#if test in phydict:
#    print phydict[test]

kodict = {}

for line in kl:
    if korx.match(line):
        pattern = korx.match(line)
        qname = pattern.group(1)
        koid = pattern.group(2)
        if qname in phydict:
            #print qname, koid, phydict[qname][6] #rank is at 'order'
            print qname, koid, phydict[qname][10] #rank is at 'phylum'
        else:
            print qname, koid, "-"

#for line in ml:


phymmbl.close()
kog.close()
#mod.close()




